package com.pedro.streamer.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.google.gson.Gson;
import com.pedro.streamer.R;
import com.pedro.streamer.manager.MediaPlayerClient;
import com.pedro.streamer.model.AudioItem;
import com.tencent.mmkv.MMKV;

import java.util.ArrayList;
import java.util.List;

public class AiRadioSupportListAdapter extends RecyclerView.Adapter<AIRadioListAdapter.TopTabViewHolder> {

    private static final String TAG = "com.bmit.engineermode";
    private Context mContext;
    private List<AudioItem> audioItemList;
    private MediaPlayerClient.PlayMode mPlayMode = MediaPlayerClient.PlayMode.ListPlay;
    private long delayTime = 10000;
    private Gson gson;
    private MediaPlayerClient mediaPlayerClient;


    public AiRadioSupportListAdapter(Context mContext) {
        this.mContext = mContext;
        this.audioItemList = new ArrayList<>();
        this.gson = new Gson();

        mediaPlayerClient = MediaPlayerClient.getInstance(mContext);
    }


    public void setAudioItemList(List<AudioItem> audioItemList) {

        this.audioItemList.clear();
        this.audioItemList.addAll(audioItemList);
        MMKV.defaultMMKV().encode("support", this.gson.toJson(this.audioItemList));
        notifyDataSetChanged();

        Log.d(TAG, "setAudioItemList: ");
        mediaPlayerClient.updatePlayList(audioItemList);
    }

    public void updateRandomState(MediaPlayerClient.PlayMode playMode) {
        mPlayMode = playMode;
        mediaPlayerClient.setPlayMode(playMode);
    }

    public void updateDelayTime(int time) {
        delayTime = time * 1000;
        mediaPlayerClient.setDelayTime(delayTime);
    }

    @Override
    public AIRadioListAdapter.TopTabViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.radio_list_layout, parent, false);

        return new AIRadioListAdapter.TopTabViewHolder(view);
    }

    static class TopTabViewHolder extends RecyclerView.ViewHolder {

        View contentView;
        TextView title_text;

        public TopTabViewHolder(View view) {
            //父类构造函数
            super(view);
            contentView = view.findViewById(R.id.content_view);
            title_text = view.findViewById(R.id.title_text);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull AIRadioListAdapter.TopTabViewHolder holder, @SuppressLint("RecyclerView") int position) {

        holder.title_text.setText(this.audioItemList.get(position).getAudioName());
        holder.contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "点击了: " + audioItemList.get(position).getAudioName());

            }
        });

        holder.item_delete_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                audioItemList.remove(position);
                notifyDataSetChanged();
                MMKV.defaultMMKV().encode("support", gson.toJson(audioItemList));
                mediaPlayerClient.updatePlayList(audioItemList);
            }
        });

    }

    public void playOrPause(boolean isOpen) {
        if (audioItemList == null || audioItemList.size() == 0) {
            Toast.makeText(mContext, "请先添加播放内容", Toast.LENGTH_LONG);
            return;
        }
        mediaPlayerClient.playRandomSong(isOpen);
    }

    public void changeVolume(float volume) {
        mediaPlayerClient.updateMusicVolume(volume);
    }

    @Override
    public int getItemCount() {

        return audioItemList.size();
    }
}
